package com.heima.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.article.service.ArticleService;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.CustomException;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import org.apache.commons.math3.stat.descriptive.rank.Max;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ApArticServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ArticleService {

    @Resource
    private  ApArticleMapper apArticleMapper;
    @Resource
    private ApArticleContentMapper apArticleContentMapper;
    @Resource
    private ArticleFreemarkerService articleFreemarkerService;

    @Resource
    private ApArticleConfigMapper apArticleConfigMapper;


    // 单页最大加载的数字
    private final static int MAX_PAGE_SIZE = 50;
    // 单页最小加载的数字
    private final static int MIN_PAGE_SIZE = 10;

    @Override
    public List<ApArticle> load(Short loadType, ArticleHomeDto dto) {
        Integer size = dto.getSize();
        size = Math.min(size, MAX_PAGE_SIZE);
        size = Math.max(size, MIN_PAGE_SIZE);
        dto.setSize(size);

        if (StrUtil.isEmpty(dto.getTag())){
            dto.setTag("__all__");
        }

        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, loadType);

        return apArticles;
    }

    @Override
    public ResponseResult<Long> saveArticle(ArticleDto articleDto) {
        if (ObjectUtil.isEmpty(articleDto)){
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        ApArticle apArticle = BeanUtil.toBean(articleDto, ApArticle.class);

        if (ObjectUtil.isEmpty(articleDto.getId())){
            // 新增操作
            // 新增文章内容
            apArticleMapper.insert(apArticle);

            // 新增文章配置
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);

            // 新增文章内容
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(articleDto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        } else {
            // 更新操作
            this.updateById(apArticle);

            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(new QueryWrapper<ApArticleContent>().eq("article_id", articleDto.getId()));
            apArticleContent.setContent(articleDto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }
        //异步调用 生成静态文件上传到minio中
        articleFreemarkerService.buildArticleToMinIO(apArticle, articleDto.getContent());
        return ResponseResult.okResult(apArticle.getId());
    }
}
